using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
	/// <summary> 
	///Author: nnamthach@gmail.com 
	/// <summary>
    public class GadgetItemsDataAccess : Connection
    {
		#region Local Variable
        private string _strSPInsertName = "dbo.[procGadgetItems_add]";
        private string _strSPUpdateName = "dbo.[procGadgetItems_update]";
        private string _strSPDeleteName = "dbo.[procGadgetItems_delete]";
        private string _strSPGetName = "dbo.[procGadgetItems_get]";
        private string _strSPGetAllName = "dbo.[procGadgetItems_getall]";
		private string _strSPGetPages = "dbo.[procGadgetItems_getpaged]";
		private string _strSPIsExist = "dbo.[procGadgetItems_isexist]";
        private string _strTableName = "[GadgetItems]";
		private string _strSPGetTransferOutName = "dbo.[procGadgetItems_gettransferout]";
        private string _strSPGetParentName = "procGadgetItems_getparent";
        private string _strSPGetByParentName = "procGadgetItems_getbyparent";
        public string _strSPGetTypeName = "procGadgetItems_gettype";
        string _strSPGetNewName = "procGadgetItems_getnew";
		#endregion Local Variable

        #region Method
        public GadgetItemsInfo Get(
        Int32 ID,
        ref string sErr)
        {
            GadgetItemsInfo objEntr = new GadgetItemsInfo();
            connect();
            InitSPCommand(_strSPGetName);
            AddParameter(GadgetItemsInfo.Field.ID.ToString(), ID);

            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP(command);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();

            if (list.Rows.Count > 0)
                objEntr = (GadgetItemsInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return objEntr;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            GadgetItemsInfo result = new GadgetItemsInfo();
            result.ID = (dt.Rows[i][GadgetItemsInfo.Field.ID.ToString()] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i][GadgetItemsInfo.Field.ID.ToString()]));
            result.Parent = (dt.Rows[i][GadgetItemsInfo.Field.Parent.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][GadgetItemsInfo.Field.Parent.ToString()]));
            result.Name = (dt.Rows[i][GadgetItemsInfo.Field.Name.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][GadgetItemsInfo.Field.Name.ToString()]));
            result.Description = (dt.Rows[i][GadgetItemsInfo.Field.Description.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][GadgetItemsInfo.Field.Description.ToString()]));
            result.Link = (dt.Rows[i][GadgetItemsInfo.Field.Link.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][GadgetItemsInfo.Field.Link.ToString()]));
            result.Type = (dt.Rows[i][GadgetItemsInfo.Field.Type.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][GadgetItemsInfo.Field.Type.ToString()]));
            result.Rank = (dt.Rows[i][GadgetItemsInfo.Field.Rank.ToString()] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i][GadgetItemsInfo.Field.Rank.ToString()]));
            result.Target = (dt.Rows[i][GadgetItemsInfo.Field.Target.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][GadgetItemsInfo.Field.Target.ToString()]));
            result.IsDefault = (dt.Rows[i][GadgetItemsInfo.Field.IsDefault.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][GadgetItemsInfo.Field.IsDefault.ToString()]));
            result.IsNew = (dt.Rows[i][GadgetItemsInfo.Field.IsNew.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][GadgetItemsInfo.Field.IsNew.ToString()]));

            return result;
        }

        public DataTable GetAll(
        ref string sErr)
        {
            connect();
            InitSPCommand(_strSPGetAllName);
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        /// <summary>
        /// Return 1: Table is exist Identity Field
        /// Return 0: Table is not exist Identity Field
        /// Return -1: Erro
        /// </summary>
        /// <param name="tableName"></param>
        public Int32 Add(GadgetItemsInfo objEntr, ref string sErr)
        {
            int ret = -1;
            connect();
            InitSPCommand(_strSPInsertName);
            AddParameter(GadgetItemsInfo.Field.Parent.ToString(), objEntr.Parent);
            AddParameter(GadgetItemsInfo.Field.Name.ToString(), objEntr.Name);
            AddParameter(GadgetItemsInfo.Field.Description.ToString(), objEntr.Description);
            AddParameter(GadgetItemsInfo.Field.Link.ToString(), objEntr.Link);
            AddParameter(GadgetItemsInfo.Field.Type.ToString(), objEntr.Type);
            AddParameter(GadgetItemsInfo.Field.Rank.ToString(), objEntr.Rank);
            AddParameter(GadgetItemsInfo.Field.Target.ToString(), objEntr.Target);
            AddParameter(GadgetItemsInfo.Field.IsDefault.ToString(), objEntr.IsDefault);
            AddParameter(GadgetItemsInfo.Field.IsNew.ToString(), objEntr.IsNew);

            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeSPScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }

        public string Update(GadgetItemsInfo objEntr)
        {
            connect();
            InitSPCommand(_strSPUpdateName);
            AddParameter(GadgetItemsInfo.Field.ID.ToString(), objEntr.ID);
            AddParameter(GadgetItemsInfo.Field.Parent.ToString(), objEntr.Parent);
            AddParameter(GadgetItemsInfo.Field.Name.ToString(), objEntr.Name);
            AddParameter(GadgetItemsInfo.Field.Description.ToString(), objEntr.Description);
            AddParameter(GadgetItemsInfo.Field.Link.ToString(), objEntr.Link);
            AddParameter(GadgetItemsInfo.Field.Type.ToString(), objEntr.Type);
            AddParameter(GadgetItemsInfo.Field.Rank.ToString(), objEntr.Rank);
            AddParameter(GadgetItemsInfo.Field.Target.ToString(), objEntr.Target);
            AddParameter(GadgetItemsInfo.Field.IsDefault.ToString(), objEntr.IsDefault);
            AddParameter(GadgetItemsInfo.Field.IsNew.ToString(), objEntr.IsNew);

            string sErr = "";
            try
            {
                excuteSPNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string Delete(
        Int32 ID
        )
        {
            connect();
            InitSPCommand(_strSPDeleteName);
            AddParameter(GadgetItemsInfo.Field.ID.ToString(), ID);

            string sErr = "";
            try
            {
                excuteSPNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public DataTableCollection Get_Page(GadgetItemsInfo obj, string orderBy, int pageIndex, int pageSize, ref String sErr)
        {
            string whereClause = CreateWhereClause(obj);
            DataTableCollection dtList = null;
            connect();
            InitSPCommand(_strSPGetPages);

            AddParameter("WhereClause", whereClause);
            AddParameter("OrderBy", orderBy);
            AddParameter("PageIndex", pageIndex);
            AddParameter("PageSize", pageSize);

            try
            {
                dtList = executeCollectSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return dtList;
        }

        public Boolean IsExist(
        Int32 ID
        )
        {
            connect();
            InitSPCommand(_strSPIsExist);
            AddParameter(GadgetItemsInfo.Field.ID.ToString(), ID);

            string sErr = "";
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            if (list.Rows.Count == 1)
                return true;
            return false;
        }

        private string CreateWhereClause(GadgetItemsInfo obj)
        {
            String result = "";

            return result;
        }

        public DataTable Search(string columnName, string columnValue, string condition, string tableName, ref string sErr)
        {
            string query = "select * from " + _strTableName + " where " + columnName + " " + condition + " " + columnValue;
            DataTable list = new DataTable();
            connect();
            try
            {
                list = executeSelectQuery(query);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            //    if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public DataTable GetTransferOut(string dtb, object from, object to, ref string sErr)
        {
            connect();
            InitSPCommand(_strSPGetTransferOutName);
            AddParameter("DB", dtb);
            AddParameter("FROM", from);
            AddParameter("TO", to);
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        #endregion Method


        public DataTable GetByGadget(string parent,string type, ref string sErr)
        {
            connect();
            InitSPCommand(_strSPGetByParentName);
            AddParameter("Parent", parent);
            AddParameter("Type", type);
            
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }

        public DataTable GetParentName(string type,ref string sErr)
        {
            connect();
            InitSPCommand(_strSPGetParentName);
            AddParameter("Type", type);
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }

        public DataTable GetByType(string type, ref string sErr)
        {
            connect();
            InitSPCommand(_strSPGetTypeName);
            AddParameter("Type", type);
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }

        public DataTable GetNew(string type, ref string sErr)
        {
            connect();
            InitSPCommand(_strSPGetNewName);
            AddParameter("Type", type);
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
    }
}
